﻿using Microsoft.Office.InfoPath;
using System.Windows.Forms;

namespace InfoPathHelpers.Validation
{
    /// <summary>
    /// Provides a number of methods for validating different data types.
    /// Note: These methods should only be called from a validation event.
    /// </summary>
    public static partial class ValidationHelper
    {
        /// <summary>
        /// Shows a MessageBox if showDialog == true, and adds a new FormError to the Errors collection.
        /// Note: Insert operations will not show a dialog because they have not yet received any data from the user.
        /// </summary>
        /// <param name="eventArgs">The XmlValidatingEventArgs for the validation event.</param>
        /// <param name="message">A ValidationMessage providing the details for both MessageBoxes and FormErrors</param>
        /// <param name="showDialog">True if a MessageBox should be shown, otherwise false.</param>
        private static void HandleValidationFailure(XmlValidatingEventArgs eventArgs, ValidationMessage message, bool showDialog)
        {
            // If the caller specified that a message should be show we do it now.
            // However, even if showDialog is true we should not show 
            // if the operation is "insert" i.e a new element.
            if (eventArgs.Operation != XmlOperation.Insert && showDialog)
                ShowValidationWarning(message);
            // We then report the error which adds it to the form errors collection.
            ReportError(eventArgs, message);
        }

        /// <summary>
        /// Shows a message box with the supplied text and caption, and just an "OK" button and "Warning" icon.
        /// </summary>
        /// <param name="message">ValidationMessage containing the text and caption 
        /// to be displayed in the message box.</param>
        private static void ShowValidationWarning(ValidationMessage message)
        {
            MessageBox.Show(message.MessageDetails, message.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        public static void ShowDefaultValidationWarning()
        {
            const string DEFAULT_VALIDATION_MESSAGE = "InfoPath cannot complete this action, because the form contains validation errors.\r\n\r\nErrors are marked with either a red asterisk (required fields) or a red, dashed border (invalid values).";
            const string DEFAULT_VALIDATION_CAPTION = "Microsoft InfoPath";

            var ValidationMessage = new ValidationMessage()
            {
                Caption = DEFAULT_VALIDATION_CAPTION,
                MessageDetails = DEFAULT_VALIDATION_MESSAGE
            };

            ShowValidationWarning(ValidationMessage);
        }

        /// <summary>
        /// Adds a FormError to the FormErrorsCollection of the form using the ReportError
        /// method of the XmlValidatingEventArgs passed in.
        /// </summary>
        /// <param name="eventArgs">The XmlValidatingEventArgs for the validation event.</param>
        /// <param name="message">ValidationMessage containing the Message and MessageDetails 
        /// to be used when creating the FormError.</param>
        private static void ReportError(XmlValidatingEventArgs eventArgs, ValidationMessage message)
        {
            eventArgs.ReportError(eventArgs.Site, false, message.Message, message.MessageDetails);
        }   
    }
}
